<?php

use think\migration\Migrator;

@set_time_limit(0);
@ini_set('memory_limit', -1);

/**
 * 本地种子库
 */
class TorrentBank extends Migrator
{
    public function change()
    {
        $this->create_torrent_bank();
    }

    private function create_torrent_bank()
    {
        $table = 'torrent_bank';

        // 存在则跳过
        if ($this->hasTable($table)) return;

        $this->table($table,[
            'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '种子库',
        ])
             ->addColumn('site_url', 'string', ['limit' => 100, 'default' => '', 'null' => true, 'comment' => '站点地址'])
             ->addColumn('pieces_hash', 'string', ['limit' => 100, 'default' => '', 'null' => true, 'comment' => 'pieces_hash'])
             // maybe个别站点id非数字
             ->addColumn('torrent_id', 'string', ['limit' => 100, 'default' => '', 'null' => true, 'comment' => 'torrent_id'])
             ->addColumn('create_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'null' => true, 'comment' => '创建时间'])
             ->addIndex('site_url', ['name' => 'idx_torrent_bank_site_url'])
             ->addIndex('pieces_hash', ['name' => 'idx_torrent_bank_pieces_hash'])
             ->create();

        // 修改主键长度
        $this->table($table)->changeColumn('id', 'integer', ['limit' => 11, 'identity' => true]);
    }
}
